﻿using System;
using System.Data;
using System.Collections;
using System.Data.SqlClient;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using Microsoft.ApplicationBlocks.Data;
namespace AnyOffice.general.report.info
{
    public partial class index : ModulePage
    {
        private void BindReportData()
        {
            string[] _years = { (DateTime.Now.Year - 1).ToString(), DateTime.Now.Year.ToString(), (DateTime.Now.Year + 1).ToString() };

            foreach (string _str in _years)
            {
                ddYear.Items.Add(new ListItem(_str, _str));
            }

            ddYear.Items.Insert(0, new ListItem("全部", "-1"));
            //if (ddYear.Items.FindByValue(DateTime.Now.Year.ToString()) != null)
            //{
            //    ddYear.ClearSelection();
            //    ddYear.Items.FindByValue(DateTime.Now.Year.ToString()).Selected = true;
            //}
            

            string commandText = @"select planid,[name] from Plan_Work where  CompletedPercent<>10 and Status<>2  ORDER BY [Plan_Work].[PlanId] DESC";
            DataTable table = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText).Tables[0];

            if (table.Rows.Count > 0)
            {
                ddlProductName.DataSource = table;
                ddlProductName.DataValueField = "planid";
                ddlProductName.DataTextField = "name";
                this.ddlProductName.DataBind();
            }
            ddlProductName.Items.Insert(0, new ListItem("全部", "-1"));

            string[] _months = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" };
            foreach (string _str in _months)
            {
                if (Convert.ToInt16(_str) <= DateTime.Now.Month)
                {
                    ddlMonth.Items.Add(new ListItem(_str, _str));
                }
            }
            //if (ddlMonth.Items.FindByValue(DateTime.Now.Month.ToString()) != null)
            //{
            //    ddlMonth.ClearSelection();
            //    ddlMonth.Items.FindByValue(DateTime.Now.Month.ToString()).Selected = true;
            //}

            ddlMonth.Items.Insert(0, new ListItem("全部", "-1"));
        }
        protected void Page_Load(object sender, EventArgs e)
        {
          

            if (!this.Page.IsPostBack)
            {
                BindReportData();
                
                string userId = (Request.QueryString["user_id"] == null) ? "" : Request.QueryString["user_id"].ToString();
                this.ViewState["UserId"] = userId;
                if (!SecurityManager.IsCanManage(userId))
                {
                    base.CloseConn();
                    Response.Write(base.ShowErrorNoNav("无权限"));
                    return;
                }
               
            }
            if (this.Page.IsPostBack)
            {
                this.pnlDiary.Visible = false;
               // this.pnlMain.Visible = false;
                this.pnlResult.Visible = true;
                ArrayList list = new ArrayList();
                StringBuilder builder = new StringBuilder();
                builder.Append("SELECT  ");
                builder.Append("\t[Report_work].[ReportId], ");
                builder.Append("\t[Report_work].[Title], ");
                builder.Append("\t[Report_work].[Type], ");
                builder.Append("\t[Report_work].[ReportTime], ");
                builder.Append("\t[Users].[UserName], ");
                builder.Append("\t[Department].[DeptName] ");
                builder.Append("FROM ");
                builder.Append("\t[dbo].[Report_work], ");
                builder.Append("\t[dbo].[Users], ");
                builder.Append("\t[dbo].[Department] ");
                builder.Append("WHERE ");
                builder.Append("\t[Report_work].[UserId] = [Users].[UserId] ");
                builder.Append("\tAND [Users].[DeptId] = [Department].[DeptId] ");
            //    builder.Append("    AND Report_work.UserId = '" + this.ViewState["UserId"].ToString() + "' ");
                if (this.ddYear.SelectedValue != "-1")
                {
                    builder.Append(" AND Report_work.Year = @Year ");
                    list.Add(new object[] { "@Year", Convert.ToInt16(this.ddYear.SelectedValue) });
                }
                if (this.ddlMonth.SelectedValue != "-1")
                {
                    builder.Append(" AND Report_work.Month = @Month ");
                    list.Add(new object[] { "@Month", Convert.ToInt16(this.ddlMonth.SelectedValue) });
                }
                if (this.ddlProductName.SelectedValue !="-1")
                {
                    builder.Append(" AND Report_work.PlanID=@PlanID  ");
                    list.Add(new object[] { "@PlanID", this.ddlProductName.SelectedValue });
                }
                builder.Append(" ORDER BY Report_work.ReportTime DESC");
                SqlParameter[] commandParameters = new SqlParameter[list.Count];
                for (int i = 0; i < list.Count; i++)
                {
                    object[] objArray4 = (object[])list[i];
                    commandParameters[i] = new SqlParameter(objArray4[0].ToString(), objArray4[1]);
                }
                DataTable table2 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, builder.ToString(), commandParameters).Tables[0];
                if (table2.Rows.Count > 0)
                {
                    this.rptQuery.DataSource = table2;
                    this.rptQuery.DataBind();
                }
                else
                {
                    this.litMessage.Text = base.ShowMessage("无符合条件的月报");
                }
            }
        }


        /// <summary>
        /// 根据类型编号，得到类型名称
        /// </summary>
        /// <param name="typeid"></param>
        /// <returns></returns>
        public string getTypeNameByTypeID(string typeid)
        {
            string returnstr = string.Empty;

            if (typeid == "1")
            {
                returnstr = "月汇报";

            }
            else if (typeid == "2")
            {
                returnstr = "季汇报";

            }
            else if (typeid == "3")
            {
                returnstr = "年汇报";

            }

            return returnstr;

        }

        //导出excel表格
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "utf-8";
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("汇报汇总", System.Text.Encoding.UTF8) + ".xls");
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");     //设置输出流为简体中文
            Response.ContentType = "application/ms-excel";    //设置输出文件类型为excel文件。
            this.EnableViewState = false;
            System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            rptQuery.RenderControl(oHtmlTextWriter);
            Response.Write(oStringWriter.ToString());
            Response.End();

        }



        /// <summary>
        /// 根据类型编号，得到类型名称
        /// </summary>
        /// <param name="typeid"></param>
        /// <returns></returns>
        public string getJsNameByTypeID(string typeid)
        {
            string returnstr = string.Empty;

            if (typeid == "1")
            {
                returnstr = "open_month";

            }
            else if (typeid == "2")
            {
                returnstr = "open_week";

            }
            else if (typeid == "3")
            {
                returnstr = "open_year";

            }

            return returnstr;

        }
 
    }
}
